public class Solution {
    public static void main(String[] args) {
	Solution s = new Solution();
	int[] prices = null;
	if (args.length >= 1) {
		prices = new int[args.length];
		for (int i = 0; i < args.length; ++i)
			prices[i] = Integer.parseInt(args[i]);
	} else {
		prices = new int[10000];
		for (int i = 0; i < args.length; ++i)
			prices[i] = 10000 - i;
	}

	int result = s.maxProfit(prices);
	System.out.println(result);
    }

    public int maxProfit(int[] prices) {
        //we really need to record the min of the prices[0 ... i], i = 0 ... prices.length - 1
        int[] min = new int[prices.length];
        //and the max of the prices[i ... prices.length - 1], i = 0 ... prices.length - 1
        int[] max = new int[prices.length];
        
        min[0] = prices[0];
        for (int i = 1; i < prices.length; ++i) {
            if (prices[i] < min[i-1])
                min[i] = prices[i];
            else
                min[i] = min[i-1];
        }
        
        max[prices.length - 1] = prices[prices.length-1];
        for (int i = 0; i < prices.length-1; ++i) {
            if (prices[i] > max[i+1])
                max[i] = prices[i];
            else
                max[i] = max[i+1];
        }
        
        int max_profit = 0;
        for (int split = 0; split < prices.length; ++split) {
            int m1 = maxProfitHelper1(prices, split, min);
            int m2 = maxProfitHelper2(prices, split, max);
            if (m1 + m2 > max_profit)
                max_profit = m1 + m2;
        }
        
        return max_profit;
    }
    
    //assming that only one transaction is allowed in subarray prices[0 ... end]
    //return the max profit possible    
    private int maxProfitHelper1(int[] prices, int end, int[] min) {
        int max_profit = 0;
        int sell = 0;
        for (sell++; sell <= end; ++sell) {
            if (prices[sell] - min[sell] > max_profit)
                max_profit = prices[sell] - min[sell];
        }
        return max_profit;
    }
    
    //assming that only one transaction is allowed in subarray prices[start ... prices.length - 1]
    //return the max profit possible
    private int maxProfitHelper2(int[] prices, int start, int[] max) {
        int max_profit = 0;
        int buy = prices.length - 1;
        for (--buy; buy >= start; --buy) {
            if (max[buy] - prices[buy] > max_profit)
                max_profit = max[buy] - prices[buy];
        }
        return max_profit;
    }

/*////Time Limit Exceeded when prices contains 10000 elements.
    public int maxProfit(int[] prices) {
        if (prices.length <= 1)
            return 0;
            
        int max_profit = 0;
        for (int split = 0; split < prices.length; split++) {
            int max1 = maxProfitHelper(prices, 0, split);
            int max2 = maxProfitHelper(prices, split, prices.length-1);
            if (max1 + max2 > max_profit)
                max_profit = max1 + max2;
        }
        
        return max_profit;
    }
    
    //assming that only one transaction is allowed in subarray prices[start ... end]
    //return the max profit possible 
    private int maxProfitHelper(int[] prices, int start, int end) {
        if (end <= start)
            return 0;
        
        int max_profit = 0;
        
        //we use a max to denote the max sell prices between prices[buy] to prices[prices.length - 1]
        int max_sell_price = prices[end];
        
        int buy = end;
        
        for (--buy; buy >= start; --buy) {
            if (prices[buy] > max_sell_price)
                max_sell_price = prices[buy];
            int profit = max_sell_price - prices[buy];
            if (profit > max_profit)
                max_profit = profit;
        }
        
        return max_profit;
    }
    
////*/
}
